class PendingusersController < ApplicationController
  before_filter :authoriz, :except => :new
  # GET /pendingusers
  # GET /pendingusers.xml
  def index
    if authorize
      @pendingusers = Pendinguser.paginate :per_page => 10, :page => params[:page]
      
      respond_to do |format|
        format.html # index.html.erb
        format.xml  { render :xml => @pendingusers }
      end
    end
  end
  
  def aceptar
    if authorize
      @user = User.new
      @pending = Pendinguser.find(params[:pendinghorse])
      @user.first_name = @pending.first_name
      @user.last_name = @pending.last_name
      @user.email = @pending.email
      @user.country = @pending.country
      @user.state = @pending.state
      @user.city = @pending.city
      @user.company = @pending.company
      @user.password_hash = @pending.password_hash
      @user.password_salt = @pending.password_salt 
      @user.user_status_id = 2
      respond_to do |format|
        if @user.save
          flash[:notice] = "#{@user.first_name} was successfully created."
          @pending.destroy
          format.html { redirect_to(:controller => "horses", :action => "index") }
          #        format.xml  { render :xml => @pendinghorse, :status => :created, :location => @pendinghorse }
        else
          #        format.html { render :action => "new" }
          #        format.xml  { render :xml => @pendinguser.errors, :status => :unprocessable_entity }
        end
      end
    end
  end
  
  # GET /pendingusers/1
  # GET /pendingusers/1.xml
  def show
    @pendinguser = Pendinguser.find(params[:id])
    
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @pendinguser }
    end
  end
  
  # GET /pendingusers/new
  # GET /pendingusers/new.xml
  def new
    @pendinguser = Pendinguser.new
    
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @pendinguser }
    end
  end
  
  # GET /pendingusers/1/edit
  def edit
    if authorize
      @pendinguser = Pendinguser.find(params[:id])
    end
  end
  
  # POST /pendingusers
  # POST /pendingusers.xml
  def create
    @pendinguser = Pendinguser.new(params[:pendinguser])
    
    respond_to do |format|
      if @pendinguser.save
        flash[:notice] = 'Pendinguser was successfully created.'
        format.html { redirect_to(@pendinguser) }
        format.xml  { render :xml => @pendinguser, :status => :created, :location => @pendinguser }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @pendinguser.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  # PUT /pendingusers/1
  # PUT /pendingusers/1.xml
  def update
    @pendinguser = Pendinguser.find(params[:id])
    
    respond_to do |format|
      if @pendinguser.update_attributes(params[:pendinguser])
        flash[:notice] = 'Pendinguser was successfully updated.'
        format.html { redirect_to(@pendinguser) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @pendinguser.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  # DELETE /pendingusers/1
  # DELETE /pendingusers/1.xml
  def destroy
    if authorize
      @pendinguser = Pendinguser.find(params[:id])
      @pendinguser.destroy
      
      respond_to do |format|
        format.html { redirect_to(pendingusers_url) }
        format.xml  { head :ok }
      end
    end
  end
  
  protected
  def authorize
    unless session[:user_id]
      flash[:notice] = "<div class='invisible'>Por favor identifiquese<div>"
      redirect_to :controller => 'admin', :action => 'login', :class => 'nyroModal', :rev=>'gal'
    else
      user = User.find(session[:user_id])
      if user.user_tipe_id == 1
        return true        
      else
        return false
      end
    end
  end
  
  protected
  def authoriz
    unless session[:user_id]
      flash[:notice] = "<div class='invisible'>Por favor identifiquese<div>"
      redirect_to :controller => 'public', :action => 'index'
    else
      @user = User.find(session[:user_id])
      unless @user
        flash[:notice] = "<div class='invisible'>Por favor identifiquese<div>"
        redirect_to :controller => 'public', :action => 'index'
      end
    end
  end
end
